﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace lcreport.Web
{
    public partial class nm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (Session["user"] == null)
            //{
            //    Maticsoft.Common.MessageBox.ShowAndRedirect(this, "请先登录！", "login.aspx");

            //}
            //else
            //{
                Page.DataBind();
            //}


        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                string code = txtCode.Text.Trim();
                if (Session["CheckCode"] != null && code.ToLower() == Session["CheckCode"].ToString().ToLower())
                {
                    int uid = 0;
                    string rname = this.txtrname.Text;
                    string rtel = this.txtrtel.Text;
                    string remail = this.txtremail.Text;
                    string rdiqu = this.txtrdiqu.Text;
                    string radd = this.txtradd.Text;
                    string rssd = "";
                    string rwork = this.txtrwork.Text;
                    string robject = this.hfrobject.Value;
                    string rtype = this.hfrtype.Value;
                    string rtypesub = this.hfrtypesub.Value;
                    string rtitle = this.txtrtitle.Text;
                    string rtext = this.txtcontent.Text;
                    DateTime rdate = DateTime.Now;
                    string fpath1 = "";
                    string fpath2 = "";
                    string fpath3 = "";
                    string fpath4 = "";
                    string fpath5 = "";
                    if (rtitle.Contains("'"))
                        return;
                    if (rname.Contains("'"))
                        return;
                    if (rtel.Contains("'"))
                        return;
                    if (remail.Contains("'"))
                        return;
                    if (rdiqu.Contains("'"))
                        return;
                    if (radd.Contains("'"))
                        return;
                    if (rwork.Contains("'"))
                        return;
                    if (robject.Contains("'"))
                        return;
                    if (rtype.Contains("'"))
                        return;
                    if (rtypesub.Contains("'"))
                        return;                    
                    if (this.FileUpload1.HasFile)//检查是否有文件
                    {
                        string fullFileName = this.FileUpload1.PostedFile.FileName;                           //文件路径名
                        string fileName = fullFileName.Substring(fullFileName.LastIndexOf("\\") + 1);   //图片名称
                        string type = fullFileName.Substring(fullFileName.LastIndexOf(".") + 1).ToLower();        //图片格式

                        if (type == "doc" || type == "jpg" || type == "gif" || type == "txt" || type == "pdf" || type == "rm") //判断是否为图片类型.doc、.jpg、.gif、.txt、.pdf、.rm
                        {
                            if (this.FileUpload1.PostedFile.ContentLength > 2 * 1024 * 1024)
                            {
                                Response.Write("<script>alert('上传附件必须小于2M！');</script>");
                                return;
                            }
                            else
                            {
                                string path = HttpContext.Current.Request.MapPath("~/upload/");//获取上传文件的网站目录路径
                                fileName = DateTime.Now.Ticks.ToString() + "." + type;
                                this.FileUpload1.SaveAs(path + fileName);//存储文件到磁盘
                                fpath1 = "/upload/" + fileName;
                            }
                        }
                        else
                        {
                            Response.Write("<script>alert('类型不对，不允许上传！');</script>");
                            return;
                        }
                    }
                    if (this.FileUpload2.HasFile)//检查是否有文件
                    {
                        string fullFileName = this.FileUpload2.PostedFile.FileName;                           //文件路径名
                        string fileName = fullFileName.Substring(fullFileName.LastIndexOf("\\") + 1);   //图片名称
                        string type = fullFileName.Substring(fullFileName.LastIndexOf(".") + 1).ToLower();        //图片格式

                        if (type == "doc" || type == "jpg" || type == "gif" || type == "txt" || type == "pdf" || type == "rm") //判断是否为图片类型.doc、.jpg、.gif、.txt、.pdf、.rm
                        {
                            if (this.FileUpload2.PostedFile.ContentLength > 2 * 1024 * 1024)
                            {
                                Response.Write("<script>alert('上传附件必须小于2M！');</script>");
                                return;
                            }
                            else
                            {
                                string path = HttpContext.Current.Request.MapPath("~/upload/");//获取上传文件的网站目录路径
                                fileName = DateTime.Now.Ticks.ToString() + "2." + type;
                                this.FileUpload2.SaveAs(path + fileName);//存储文件到磁盘
                                fpath2 = "/upload/" + fileName;
                            }
                        }
                        else
                        {
                            Response.Write("<script>alert('类型不对，不允许上传！');</script>");
                            return;
                        }
                    }
                    if (this.FileUpload3.HasFile)//检查是否有文件
                    {
                        string fullFileName = this.FileUpload3.PostedFile.FileName;                           //文件路径名
                        string fileName = fullFileName.Substring(fullFileName.LastIndexOf("\\") + 1);   //图片名称
                        string type = fullFileName.Substring(fullFileName.LastIndexOf(".") + 1).ToLower();        //图片格式

                        if (type == "doc" || type == "jpg" || type == "gif" || type == "txt" || type == "pdf" || type == "rm") //判断是否为图片类型.doc、.jpg、.gif、.txt、.pdf、.rm
                        {
                            if (this.FileUpload3.PostedFile.ContentLength > 2 * 1024 * 1024)
                            {
                                Response.Write("<script>alert('上传附件必须小于2M！');</script>");
                                return;
                            }
                            else
                            {
                                string path = HttpContext.Current.Request.MapPath("~/upload/");//获取上传文件的网站目录路径
                                fileName = DateTime.Now.Ticks.ToString() + "3." + type;
                                this.FileUpload3.SaveAs(path + fileName);//存储文件到磁盘
                                fpath3 = "/upload/" + fileName;
                            }
                        }
                        else
                        {
                            Response.Write("<script>alert('类型不对，不允许上传！');</script>");
                            return;
                        }
                    }
                    if (this.FileUpload4.HasFile)//检查是否有文件
                    {
                        string fullFileName = this.FileUpload4.PostedFile.FileName;                           //文件路径名
                        string fileName = fullFileName.Substring(fullFileName.LastIndexOf("\\") + 1);   //图片名称
                        string type = fullFileName.Substring(fullFileName.LastIndexOf(".") + 1).ToLower();        //图片格式

                        if (type == "doc" || type == "jpg" || type == "gif" || type == "txt" || type == "pdf" || type == "rm") //判断是否为图片类型.doc、.jpg、.gif、.txt、.pdf、.rm
                        {
                            if (this.FileUpload4.PostedFile.ContentLength > 2 * 1024 * 1024)
                            {
                                Response.Write("<script>alert('上传附件必须小于2M！');</script>");
                                return;
                            }
                            else
                            {
                                string path = HttpContext.Current.Request.MapPath("~/upload/");//获取上传文件的网站目录路径
                                fileName = DateTime.Now.Ticks.ToString() + "4." + type;
                                this.FileUpload4.SaveAs(path + fileName);//存储文件到磁盘
                                fpath4 = "/upload/" + fileName;
                            }
                        }
                        else
                        {
                            Response.Write("<script>alert('类型不对，不允许上传！');</script>");
                            return;
                        }
                    }
                    if (this.FileUpload5.HasFile)//检查是否有文件
                    {
                        string fullFileName = this.FileUpload5.PostedFile.FileName;                           //文件路径名
                        string fileName = fullFileName.Substring(fullFileName.LastIndexOf("\\") + 1);   //图片名称
                        string type = fullFileName.Substring(fullFileName.LastIndexOf(".") + 1).ToLower();        //图片格式

                        if (type == "doc" || type == "jpg" || type == "gif" || type == "txt" || type == "pdf" || type == "rm") //判断是否为图片类型.doc、.jpg、.gif、.txt、.pdf、.rm
                        {
                            if (this.FileUpload5.PostedFile.ContentLength > 2 * 1024 * 1024)
                            {
                                Response.Write("<script>alert('上传附件必须小于2M！');</script>");
                                return;
                            }
                            else
                            {
                                string path = HttpContext.Current.Request.MapPath("~/upload/");//获取上传文件的网站目录路径
                                fileName = DateTime.Now.Ticks.ToString() + "5." + type;
                                this.FileUpload5.SaveAs(path + fileName);//存储文件到磁盘
                                fpath1 = "/upload/" + fileName;
                            }
                        }
                        else
                        {
                            Response.Write("<script>alert('类型不对，不允许上传！');</script>");
                            return;
                        }
                    }

                    lcreport.Model.reportinfo model = new lcreport.Model.reportinfo();

                    model.rname = rname;
                    model.rtel = rtel;
                    model.remail = remail;
                    model.rdiqu = rdiqu;
                    model.radd = radd;
                    model.rssd = rssd;
                    model.rwork = rwork;
                    model.robject = robject;
                    model.rtype = rtype;
                    model.rtypesub = rtypesub;
                    model.rtitle = rtitle;
                    model.rtext = rtext;
                    model.rdate = rdate;
                    model.uid = uid;
                    model.rfile1 = fpath1;
                    model.rfile2 = fpath2;
                    model.rfile3 = fpath3;
                    model.rfile4 = fpath4;
                    model.rfile5 = fpath5;


                    lcreport.BLL.reportinfo bll = new lcreport.BLL.reportinfo();
                    int i = bll.Add(model);
                    if (i > 0)
                    {
                        string spwd = i.ToString() + RndNum(6);
                        bll.Updatespwd(i, spwd);
                        Maticsoft.Common.MessageBox.ShowAndRedirect(this, "举报成功，请记住查询码为：" + spwd, "xz.aspx");
                    }
                }
                else
                {
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "a", "alert('验证码错误！');", true);

                }
            }
            
        }
        public string RndNum(int VcodeNum)
        {
            System.Random rand = new Random();
            char[] chars = "0123456789abcdefghijklmnopqrstuvwxyx".ToCharArray();

            System.Text.StringBuilder myStr = new System.Text.StringBuilder();
            for (int iCount = 0; iCount < VcodeNum; iCount++)
            {
                myStr.Append(chars[rand.Next(chars.Length)]);
            }
            return myStr.ToString();

        }
    }
}